import java.util.Scanner;

public class FarmManagementSystem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        EnvironmentSensor sensor = new EnvironmentSensor();
        FarmMachineObserver seederObserver = new FarmMachineObserver("播种机");
        FarmMachineObserver sprayerObserver = new FarmMachineObserver("喷药机");
        sensor.addObserver(seederObserver);
        sensor.addObserver(sprayerObserver);

        while (true) {
            System.out.println("请选择功能:");
            System.out.println("1. 创建农机设备");
            System.out.println("2. 监测环境变化");
            System.out.println("3. 路径规划");
            System.out.println("4. 农机状态管理");
            System.out.println("5. 农机GPS增强（装饰模式）");
            System.out.println("6. 退出");
            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    System.out.println("请输入农机类型（播种机/喷药机）:");
                    String type = scanner.next();
                    FarmMachine machine = FarmMachineFactory.createFarmMachine(type);
                    Command command = type.equals("播种机") ? new SeederCommand(machine) : new SprayerCommand(machine);
                    CommandInvoker invoker = new CommandInvoker();
                    invoker.setCommand(command);
                    invoker.invoke();
                    break;
                case 2:
                    sensor.changeDetected();
                    break;
                case 3:
                    PathPlanner planner = new PathPlanner();
                    System.out.println("选择路径规划策略（1. 动态路径规划 2. 避障路径规划）:");
                    int strategyChoice = scanner.nextInt();
                    if (strategyChoice == 1) {
                        planner.setStrategy(new DynamicPathPlanning());
                    } else {
                        planner.setStrategy(new ObstacleAvoidancePlanning());
                    }
                    planner.executePlan();
                    break;
                case 4:
                    FarmMachineContext context = new FarmMachineContext();
                    System.out.println("选择农机状态（1. 空闲 2. 工作）:");
                    int stateChoice = scanner.nextInt();
                    if (stateChoice == 1) {
                        context.setState(new IdleState());
                    } else {
                        context.setState(new WorkingState());
                    }
                    context.request();
                    break;
                case 5:
                    System.out.println("请输入农机类型（播种机/喷药机）:");
                    String machineType = scanner.next();
                    FarmMachine baseMachine = FarmMachineFactory.createFarmMachine(machineType);
                    if (baseMachine != null) {
                        FarmMachine gpsEnhancedMachine = new GPSDecorator(baseMachine);
                        System.out.println("\nGPS增强后的农机开始工作:");
                        gpsEnhancedMachine.work();
                    }
                    break;
                case 6:
                    System.out.println("退出系统...");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效选择，请重试。");
            }
        }
    }
}